package framework.attack;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;

public class Teleport extends BasicAttack<BasicFighter> {
	
	protected Image teleportForward;
	
	protected Image teleportBackward;
	
	protected Image teleportUsed;
	
	protected int initialX;
	
	protected boolean forward;

	public Teleport(BasicFighter fighter) {
		setAttacker(fighter);
		this.teleportForward = fighter.getAnimations().getSpriteSheet().getSprite(0, 0);
		this.teleportBackward = this.teleportForward.getFlippedCopy(true, false);
		initialX = 0;
	}

	@Override
	public void trigger() {
		BasicFighter a = getAttacker();
		if (a.getNbTeleport() > 0) {
			super.trigger();
			teleportUsed = a.isForward() ? teleportForward : teleportBackward;
			initialX = (int) a.getHitBox().getCenterX();
			forward = a.isForward() && a.getXspeed() == 0 || a.getXspeed() > 0;
			a.setNbTeleport(Math.max(0, a.getNbTeleport() - 1));
			if (a.isJumping()) {
				a.setYspeed(Math.abs(a.getYspeed()));
			}
		}
	}

	@Override
	public void draw(Graphics g) {
		BasicFighter f = getAttacker();
		if (currentTime <= getDuration() / 2) {
			this.teleportUsed.setAlpha(0.5f - ((float)currentTime / (float)getDuration()));
			this.teleportUsed.draw(f.getX(), f.getY(), f.getWidth(), f.getHeight());
		} else {
			if (getAttacker().isForward()) {
				this.teleportForward.setAlpha(-0.5f + ((float)currentTime / (float)getDuration()));
				this.teleportForward.draw(f.getX(), f.getY(), f.getWidth(), f.getHeight());
			} else {
				this.teleportBackward.setAlpha(-0.5f + ((float)currentTime / (float)getDuration()));
				this.teleportBackward.draw(f.getX(), f.getY(), f.getWidth(), f.getHeight());
			}
		}
		
		this.teleportBackward.setAlpha(1);
	}
	
	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		int distance = gc.getWidth() / 2;
		if (currentTime >= getDuration() / 2) {
			if (forward) {
				getAttacker().setX(initialX + distance - getAttacker().getWidth());
			} else {
				System.out.println("x :" + getAttacker().getX());
				getAttacker().setX(initialX - distance);
			}
		}
	}

	@Override
	public int getDuration() {
		return 200;
	}

	@Override
	public Rectangle getHitBox() {
		return new Rectangle(0, 0, 0, 0);
	}
	
	@Override
	public boolean isDrawingAttacker() {
		return isActive();
	}
	
	@Override
	public boolean isBlocking() {
		return isActive();
	}

	@Override
	public boolean isInterruptable() {
		return false;
	}
	
	
}
